<?php
/**
 * FP_Auth class file
 * @package FlickrPhotosets
 */

/**
 * This class authenticates users with Flickr.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class FP_Auth extends FlickrPhotosets {
	
	/**
	 * This constructor handles the communication between Flickr and the
	 * app when authenticating. When removing the authentication, it will
	 * prompt the user to revoke permissions from Flickr.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		
		// Remove the authorization
		if (isset($_GET['remove'])) {
			
			/*
			Run the parent constructor to create the Facebook object
			and connect to the database
			*/
			parent::__construct();
			
			// Get the user that is logged in
			$this->uid = $this->facebook->require_login();
			
			// Get the user's configuration so we can send them to remove Flickr permission
			$this->get_info();
			
			// Remove their Flickr authorization
			$this->remove_auth();
			
			// Go right to the configure page, instead of presenting option for revoke
			if (isset($_GET['skiprevoke'])) {
				$this->facebook->redirect($this->canvas_url.'/configure.php');
			}
			
		}
		// Authorize an account
		else {
			
			/*
			This is a little complicated because for some reason,
			if this is all done in a canvas page there are too
			many redirects and Facebook doesn't like it.

			So the solution is to have the first page NOT a canvas
			page, that is the one that redirects to Flickr. Flickr
			will then bounce us back to a canvas page so we can
			make sure that the user is who they say they are.

			There is no need to see if authentication has already
			taken place. It will just regenerate the same token.
			*/

			// Create the Flickr object
			$this->flickr = new phpFlickr(FLICKR_API_KEY, FLICKR_API_SECRET);

			// We can tell if we're coming from Flickr if there is a frob
			if (empty($_GET['frob'])) {

				// Since we don't have a frob, redirect to Flickr to get one
				if (isset($_GET['write'])) {
					$this->flickr->auth('write', false);
				}
				else {
					$this->flickr->auth('read', false);
				}

			}
			else {

				// Now we will run the parent constructor
				parent::__construct();

				// Make sure this is a canvas page (should never be a problem)
				$this->facebook->require_frame();

				// We will get the user that is logged in (hopefully it's the right person!)
				$this->uid = $this->facebook->require_login();

				// Now we have the frob and can generate the token and store it
				$token = $this->flickr->auth_getToken($_GET['frob']);
				$this->store_token($token);
				
				// Set the profile stuff
				$this->set_profile_fbml();
				//$this->set_info_box();

				// Redirect to the proper page
				if ($_GET['redir'] == 'index') {
					$this->facebook->redirect($this->canvas_url.'/?added');
				}
				else {
					$this->facebook->redirect($this->canvas_url.'/configure/');
				}

			}
			
		}
		
	}
	
	/**
	 * Checks the token in the supplied Flickr object to see if it is valid.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param phpFlickr $flickr The Flickr object to check with
	 * @return boolean True if the token is valid
	 */
	static public function check_token($flickr) {
		$check = $flickr->auth_checkToken();
		return ($check['perms'] == 'none' or empty($check)) ? false : true;
	}
	
	/**
	 * Gets the authorization permissions.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param phpFlickr $flickr The Flickr object to check with
	 * @return string The permissions (none, read, write, delete)
	 */
	static public function get_perms($flickr) {
		$check = $flickr->auth_checkToken();
		return $check['perms'];
	}
	
	/**
	 * Removes the user's Flickr information from the database.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return boolean True if successfully removed
	 */
	private function remove_auth() {
		return $this->_db->remove_flickr_auth($this->uid);
	}
	
	/**
	 * Stores the user's Flickr token and NSID in the database.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $return The array returned from calling the flickr->auth_getToken() function
	 * @return boolean True if successfully added
	 */
	private function store_token($return) {
		return $this->_db->store_token($this->uid, $return['user']['nsid'], $return['token']);
	}
	
}

?>